package cn.ubeta.b2c.widget.ptr;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import cn.ubeta.b2c.R;

import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrUIHandler;
import in.srain.cube.views.ptr.indicator.PtrIndicator;

/**
 * Created by Denny on 16/6/12.
 */
public class PtrRefreshHeader extends FrameLayout implements PtrUIHandler {
    private ImageView mRotateView;
    private TextView mTipsTv;
    private ProgressBar mProgressBar;

    private float mRotationPivotX, mRotationPivotY;

    public PtrRefreshHeader(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PtrRefreshHeader(Context context) {
        super(context);
        init();
    }

    private void init() {
        View headerView=LayoutInflater.from(getContext()).inflate(R.layout.item_pull_to_refresh_header, this);
        mRotateView = (ImageView) headerView.findViewById(R.id.iv_rotate_view);
        mTipsTv = (TextView) headerView.findViewById(R.id.tv_tips);
        mProgressBar= (ProgressBar) headerView.findViewById(R.id.ptr_progressbar);
        buildAnimation();
    }

    @Override
    public void onUIReset(PtrFrameLayout frame) {
        hideRotateView();
        mProgressBar.setVisibility(View.GONE);
    }

    @Override
    public void onUIRefreshPrepare(PtrFrameLayout frame) {
        mTipsTv.setText(R.string.pull_down_to_refresh);
        mProgressBar.setVisibility(GONE);
        mRotateView.setVisibility(VISIBLE);
    }

    @Override
    public void onUIRefreshBegin(PtrFrameLayout frame) {
        mTipsTv.setText(R.string.ptr_refreshing);
        hideRotateView();
        mProgressBar.setVisibility(VISIBLE);

    }

    @Override
    public void onUIRefreshComplete(PtrFrameLayout frame) {
        hideRotateView();
        mProgressBar.setVisibility(GONE);
        mTipsTv.setText("");
    }
    private void hideRotateView() {
        mRotateView.clearAnimation();
        mRotateView.setVisibility(GONE);
    }

    @Override
    public void onUIPositionChange(PtrFrameLayout frame, boolean isUnderTouch, byte status, PtrIndicator ptrIndicator) {

        float percent = ptrIndicator.getCurrentPercent();
//        if (percent >= 1.0f) {
//            mTipsTv.setText(R.string.release_to_refresh);
//        } else {
//            mTipsTv.setText(R.string.pull_down_to_refresh);
//        }

        final int mOffsetToRefresh = frame.getOffsetToRefresh();
        final int currentPos = ptrIndicator.getCurrentPosY();
        final int lastPos = ptrIndicator.getLastPosY();

        if (currentPos < mOffsetToRefresh && lastPos >= mOffsetToRefresh) {
            if (isUnderTouch && status == PtrFrameLayout.PTR_STATUS_PREPARE) {
                mTipsTv.setText(R.string.pull_down_to_refresh);
                if (mRotateView != null) {
                    mRotateView.clearAnimation();
                    mRotateView.startAnimation(mReverseFlipAnimation);
                }
            }
        } else if (currentPos > mOffsetToRefresh && lastPos <= mOffsetToRefresh) {
            if (isUnderTouch && status == PtrFrameLayout.PTR_STATUS_PREPARE) {
                mTipsTv.setText(R.string.release_to_refresh);
                if (mRotateView != null) {
                    mRotateView.clearAnimation();
                    mRotateView.startAnimation(mFlipAnimation);
                }
            }
        }
        if (percent < 0.8f) percent = 0.8f;
        if (percent > 1.2f) percent = 1.2f;
        if (status != PtrFrameLayout.PTR_STATUS_PREPARE) {
            return;
        }
    }
    private int mRotateAniTime = 150;
    private RotateAnimation mFlipAnimation;
    private RotateAnimation mReverseFlipAnimation;

    private void buildAnimation() {
        mFlipAnimation = new RotateAnimation(0, -180, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
        mFlipAnimation.setInterpolator(new LinearInterpolator());
        mFlipAnimation.setDuration(mRotateAniTime);
        mFlipAnimation.setFillAfter(true);

        mReverseFlipAnimation = new RotateAnimation(-180, 0, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
        mReverseFlipAnimation.setInterpolator(new LinearInterpolator());
        mReverseFlipAnimation.setDuration(mRotateAniTime);
        mReverseFlipAnimation.setFillAfter(true);
    }
}
